/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.sale;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.pos.I_Ticket;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.bean.Customer;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

/**
 *
 * @author Administrator
 */
public class TicketDAO extends ModifiedLogDAO<Ticket> {

    public TicketDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                  "TICKETS",
                  new Field[]{
                       new Field(I_Ticket.ID,Datas.STRING,Formats.STRING),
                       new Field(I_Ticket.TICKETTYPE,Datas.INT,Formats.INT),
                       new Field(I_Ticket.TICKETID,Datas.INT,Formats.INT),
                       new Field(I_Ticket.PERSON,Datas.STRING,Formats.STRING),
                       new Field(I_Ticket.CUSTOMER,Datas.STRING,Formats.STRING),
                       //new Field("STATUS",Datas.INT,Formats.INT),
                       new Field(I_Ticket.PRICERATE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Ticket.GROUPNAME,Datas.STRING,Formats.STRING),
                       new Field(I_Ticket.LOCATIONREF,Datas.STRING,Formats.STRING),
                       new Field(I_Ticket.CURDATE,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field(I_Ticket.LASTMODIFIED,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field(I_Ticket.TOTALPRICE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Ticket.TOTALTAX,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Ticket.TOTALCONSULTCOST,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Ticket.HOSTNAME,Datas.STRING,Formats.STRING),
                       new Field(I_Ticket.TOTALSCORE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_Ticket.SALEMANAGER,Datas.DOUBLE,Formats.DOUBLE),
                       
                      },
                  new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, Ticket obj) throws BasicException {
        dp.setString(1,obj.getId());
        dp.setInt(2,obj.getTicketType());
        dp.setInt(3,obj.getTicketId());
        dp.setString(4,obj.getUser().getId());
        dp.setString(5,obj.getCustomerId());
        dp.setDouble(6,obj.getM_dPriceRate());
        dp.setString(7,obj.getM_sGroupName());
        dp.setString(8,obj.getLocation());
        dp.setTimestamp(9,obj.getM_dDate());
        dp.setTimestamp(10,obj.getLastModified());
        dp.setDouble(11,obj.getTotalPrice());
        dp.setDouble(12,obj.getTotalTax());
        dp.setDouble(13,obj.getTotalConsultCost());
        dp.setString(14,obj.getHostName());
        dp.setDouble(15,obj.getTotalScore());
        dp.setString(16,obj.getSaleManager());
    }

    public Ticket readValues(DataRead dr,Ticket obj) throws BasicException {
        if(obj==null)
        	obj=new Ticket();
        obj.setM_sId(dr.getString(1));
        obj.setTicketType(dr.getInt(2));
        obj.setTicketId(dr.getInt(3));
        obj.setUser(new UserInfo(dr.getString(4)));
        obj.setCustomer(new Customer(dr.getString(5)));
        obj.setM_dPriceRate(dr.getDouble(6));
        obj.setM_sGroupName(dr.getString(7));
        obj.setLocation(dr.getString(8));
        obj.setM_dDate(dr.getTimestamp(9));
        obj.setLastModified(dr.getTimestamp(10));
        obj.setTotalPrice(dr.getDouble(11));
        obj.setTotalTax(dr.getDouble(12));
        obj.setTotalConsultCost(dr.getDouble(13));
        obj.setHostName(dr.getString(14));
        obj.setTotalScore(dr.getDouble(15));
        obj.setSaleManager(dr.getString(16));
        return obj;
    }

    @Override
    public Class getSuportClass() {
        return Ticket.class;
    }
    
    
    public Ticket loadTicket(final int tickettype, final int ticketid) throws BasicException{
    	return (Ticket) new PreparedSentence(s, this.getTableDefinition().getListSQL()+" WHERE TICKETTYPE = ? AND TICKETID = ?", SerializerWriteParams.INSTANCE, this).find(new DataParams(){
            public void writeValues() throws BasicException {
                setInt(1, tickettype);
                setInt(2, ticketid);
            }
        });
    }
}
